<script setup>
import { ref } from 'vue'
import { getAIChatInfo } from '@/apis/home'

// 控制对话框的显示和关闭
const isVisible = ref(false)

// 用户输入的AI关键词
const AiInput = ref(null)

// AI聊天的回复
const AIResponse = ref(null)

// 控制AI回复时加载状态的开关
const loading = ref(false)

// 获取AI交流信息
const submitAIchat = async () => {
  loading.value = true
  const res = await getAIChatInfo(AiInput.value)
  AIResponse.value = res.data.data
  AiInput.value = null
  loading.value = false
}

// 打开对话框
const openDialog = () => {
  isVisible.value = true
}

defineExpose({
  openDialog
})
</script>

<template>
  <el-dialog
    v-model="isVisible"
    style="height: 780px; background-color: #293266"
    center
    draggable
    width="1200"
  >
    <template #header>
      <h2 class="title">乐小智AI对话框</h2>
    </template>

    <div class="aiitem">
      <div class="input"></div>
      <div class="response">
        <p
          v-loading="loading"
          element-loading-text="乐小智AI正在思考中"
          element-loading-background="#293266"
        >
          <span v-html="AIResponse"></span>
        </p>
      </div>

      <div class="userInput">
        <el-input
          v-model="AiInput"
          placeholder="请输入您的问题"
          style="width: 800px; border-radius: 20px"
          size="large"
          @keyup.enter="submitAIchat"
        >
        </el-input>
        <svg @click="submitAIchat" class="icon" aria-hidden="true">
          <use xlink:href="#icon-fasong"></use>
        </svg>
      </div>
    </div>
  </el-dialog>
</template>

<style lang="scss" scoped>
.title {
  color: #ffff;
}

.aiitem {
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  padding: 20px;

  .response {
    width: 1100px;
    height: 750px;
    margin: 20px;
    max-height: 500px;
    overflow-y: auto;
    padding: 20px;
    box-sizing: border-box;

    span {
      font-size: 20px;
      color: #ffff;
    }
  }

  .userInput {
    display: flex;
    margin-top: 30px;

    .icon {
      font-size: 10px;
      margin-left: 10px;
    }
  }
}
</style>
